package com.ruoyi.system.outto.game.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.model.domain.AccountInfo;
import com.ruoyi.system.model.domain.InfoImg;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.mapper.InfoImgMapper;
import com.ruoyi.system.outto.core.LCConstant;
import com.ruoyi.system.outto.game.CommonApi;
import com.ruoyi.system.outto.game.IJIZAIAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class JiZaiApiImpl implements IJIZAIAPI {

    @Resource
    private InfoImgMapper infoImgMapper;
    @Resource
    private CommonApi commonApi;
    @Value("${desc.title}")
    private String descTitle;
    private SalePartner salePartner;


    @Override
    public AjaxResult addGame(AccountInfo accountInfo, Long userId, SalePartner salePartner) {

        this.salePartner = salePartner;

        Map<String, Object> result = new HashMap<>();

        Map<String, Object> params = params(accountInfo,salePartner);
        params.put("type","add");
        String path = "/trade/goodsApi/parseingGameToApi";

        String other = salePartner.getOther();
        log.info("鸡仔添加接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.sendPost(other + path, JSONObject.toJSONString(params), "application/json");
        log.info("鸡仔添加接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!"0".equals(resultJson.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("msg"));
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult editGame(AccountInfo accountInfo, Long userId, SalePartner salePartner) {

        this.salePartner = salePartner;

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = params(accountInfo,salePartner);
        params.put("type","update");
        String path = "/trade/goodsApi/updateAccountToApi";

        String other = salePartner.getOther();
        log.info("鸡仔编辑接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.sendPost(other + path, JSONObject.toJSONString(params), "application/json");
        log.info("鸡仔编辑接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!"0".equals(resultJson.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("msg"));
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult delGame(Long id, Long userId, SalePartner salePartner) {


        String path = "/trade/goodsApi/offlineAccountByIdToApi";

        Map<String,Object> params = new HashMap<>();
        params.put("externalId",id);
        params.put("phone",salePartner.getSaleKey());
        Map<String, Object> result = new HashMap<>();

        String other = salePartner.getOther();
        log.info("鸡仔删除接口请求参数={}", JSONObject.toJSONString(params));
//        https://xizai.maoligame.com/trade/api/offlineAccountByIdToApi?phone=18797700703&externalId=5623

        String response = HttpUtils.sendGet(other + path + "?externalId=" + id +"&phone=" + salePartner.getSaleKey(), null);
        log.info("鸡仔删除接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!"0".equals(resultJson.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("msg"));
        return AjaxResult.success(result);
    }


    private Map<String,Object> params(AccountInfo accountInfo,SalePartner sa){
        Map<String,Object> params = new HashMap<>();
        params.put("name","xyr" + accountInfo.getId() + " " + accountInfo.getLevel() + " " + accountInfo.getDes());
        params.put("aiValue", UUIDGenerator.generate12());
        params.put("aiPwd", "1");
        BigDecimal bigDecimal = commonApi.addPrice(accountInfo.getPrice(), sa.getSeleep());
        commonApi.setPrice(bigDecimal,accountInfo.getId(),sa);
        params.put("price", bigDecimal);
        params.put("whetherAdult", "1");
        String type = getType(accountInfo.getType());
        if (StringUtils.isEmpty(type)){
            throw  new GlobalException("暂不支持的游戏="+accountInfo.getType());
        }
        params.put("categoryId",type);
        InfoImg infoImg = new InfoImg();
        infoImg.setInfoId(accountInfo.getId());
        infoImg.setType(Constants.STR_1);
        List<InfoImg> infoImgs = infoImgMapper.selectInfoImgList(infoImg);
        String imgs = "";
        if (!CollectionUtils.isEmpty(infoImgs)) {
            imgs = infoImgs.stream().map(i -> i.getImgUrl()).collect(Collectors.joining(","));
        } else {
            imgs =  LCConstant.defaultImg;
        }

        params.put("imagel",imgs);
        params.put("sallerContact",salePartner.getSaleKey());//电话
        params.put("externalId",accountInfo.getId());
        params.put("sallerQq","");
        params.put("accountOrigin","1");
        params.put("antiAddiction","2");
        params.put("antiAddiction","2");

        if (Arrays.asList(LCConstant.haveTwoList).contains(accountInfo.getLevel())){
            params.put("isFace","1");
        }else if (Objects.equals(accountInfo.getLevel(), LCConstant.bakFaceTo)){
            params.put("isFace","1");
        }else {
            params.put("isFace","0");
        }
        setOther(accountInfo,params);
        return params;
    }

    private void setOther(AccountInfo accountInfo, Map<String,Object> params){
        JSONObject gameJson = new JSONObject();
        if (Objects.equals("王者荣耀",accountInfo.getType())){
            gameJson.put("wzry_hero_num",accountInfo.getHeroes());
            gameJson.put("wzry_skin_num",accountInfo.getSkins());

            if ("安卓Q".equals(accountInfo.getRegional())){
                gameJson.put("wzry_sys_area","安卓QQ");
            }
            if ("安卓WX".equals(accountInfo.getRegional())){
                gameJson.put("wzry_sys_area","安卓微信");
            }
            if ("苹果Q".equals(accountInfo.getRegional())){
                gameJson.put("wzry_sys_area","苹果QQ");
            }
            if ("苹果WX".equals(accountInfo.getRegional())){
                gameJson.put("wzry_sys_area","苹果微信");
            }


            String wz_sspf = commonApi.getSelectLable("王者荣耀", "限定皮肤", accountInfo.getAcclable());
            gameJson.put("wz_sspf",wz_sspf);
            String wzry_rare_skin = commonApi.getSelectLable("王者荣耀", "热门皮肤", accountInfo.getAcclable());
            gameJson.put("wzry_rare_skin",wzry_rare_skin);
            String wzry_rydc = commonApi.getSelectLable("王者荣耀", "荣耀典藏", accountInfo.getAcclable());
            gameJson.put("wzry_rydc",wzry_rydc);
        }
        if (Objects.equals("和平精英",accountInfo.getType())){
            gameJson.put("hpjy_suit_num",accountInfo.getSuit());
//            gameJson.put("hpjy_sys_area",accountInfo.getRegional());

            if ("安卓Q".equals(accountInfo.getRegional())){
                gameJson.put("hpjy_sys_area","安卓QQ");
            }
            if ("安卓WX".equals(accountInfo.getRegional())){
                gameJson.put("hpjy_sys_area","安卓微信");
            }
            if ("苹果Q".equals(accountInfo.getRegional())){
                gameJson.put("hpjy_sys_area","苹果QQ");
            }
            if ("苹果WX".equals(accountInfo.getRegional())){
                gameJson.put("hpjy_sys_area","苹果微信");
            }


            String hpjy_rare_weapons = commonApi.getSelectLable("和平精英", "稀有武器", accountInfo.getAcclable());
            gameJson.put("hpjy_rare_weapons",hpjy_rare_weapons);
            String hpjy_rare_suit = commonApi.getSelectLable("和平精英", "稀有套装", accountInfo.getAcclable());
            gameJson.put("hpjy_rare_suit",hpjy_rare_suit);
            String hpjy_rare_vehicle = commonApi.getSelectLable("和平精英", "稀有载具", accountInfo.getAcclable());
            gameJson.put("hpjy_rare_vehicle",hpjy_rare_vehicle);
            String hpjy_rare_aircraft = commonApi.getSelectLable("和平精英", "稀有飞行器", accountInfo.getAcclable());
            gameJson.put("hpjy_rare_aircraft",hpjy_rare_aircraft);
        }
        if (Objects.equals("原神",accountInfo.getType())){
            gameJson.put("ys_power",accountInfo.getLevel());
            String ys_five_star_weapons = commonApi.getSelectLable("原神", "五星武器", accountInfo.getAcclable());
            gameJson.put("ys_five_star_weapons",ys_five_star_weapons);
            String ys_manming = commonApi.getSelectLable("原神", "满命角色", accountInfo.getAcclable());
            gameJson.put("ys_manming",ys_manming);
        }

        if (Objects.equals("火影忍者",accountInfo.getType())){
            gameJson.put("area_ervice",accountInfo.getRegional());
            String sninja = commonApi.getSelectLable("火影忍者", "S级忍者", accountInfo.getAcclable());
            gameJson.put("sninja",sninja);
            String aninja = commonApi.getSelectLable("火影忍者", "A级忍者", accountInfo.getAcclable());
            gameJson.put("aninja",aninja);
        }

        if (Objects.equals("CF手游",accountInfo.getType())){
            gameJson.put("cf_sys_area",accountInfo.getRegional());
            String cf_king_of_weapons = commonApi.getSelectLable("CF手游", "王者武器", accountInfo.getAcclable());
            gameJson.put("cf_king_of_weapons",cf_king_of_weapons);
            String cf_main_weapons = commonApi.getSelectLable("CF手游", "主武器", accountInfo.getAcclable());
            gameJson.put("cf_main_weapons",cf_main_weapons);
            String cf_deputy_weapons = commonApi.getSelectLable("CF手游", "副武器", accountInfo.getAcclable());
            gameJson.put("cf_deputy_weapons",cf_deputy_weapons);
            String cf_hand_grenades = commonApi.getSelectLable("CF手游", "手雷", accountInfo.getAcclable());
            gameJson.put("cf_hand_grenades",cf_hand_grenades);
            String cf_rare_items = commonApi.getSelectLable("CF手游", "稀有道具", accountInfo.getAcclable());
            gameJson.put("cf_rare_items",cf_rare_items);
            String cf_rare_items_skin = commonApi.getSelectLable("CF手游", "稀有皮肤", accountInfo.getAcclable());
            gameJson.put("cf_rare_items",cf_rare_items + "," +cf_rare_items_skin);
        }

        if (Objects.equals("CF端游",accountInfo.getType())){
            gameJson.put("cf_swin_war_zone_details",accountInfo.getRegional());
            String cf_swin_king_golden_weapon = commonApi.getSelectLable("CF端游", "王者炫金武器", accountInfo.getAcclable());
            gameJson.put("cf_swin_king_golden_weapon",cf_swin_king_golden_weapon);
            String cf_swin_hot_weapons = commonApi.getSelectLable("CF端游", "热门武器", accountInfo.getAcclable());
            gameJson.put("cf_swin_hot_weapons",cf_swin_hot_weapons);
            String cf_swin_hot_skin = commonApi.getSelectLable("CF端游", "热门皮肤", accountInfo.getAcclable());
            gameJson.put("cf_swin_hot_skin",cf_swin_hot_skin);
            String cf_swin_hot_items = commonApi.getSelectLable("CF端游", "热门道具", accountInfo.getAcclable());
            gameJson.put("cf_swin_hot_items",cf_swin_hot_items);
        }
        if (Objects.equals("光遇",accountInfo.getType())){
            gameJson.put("gy_sys_area",accountInfo.getRegional());
            String gy_zhenxi = commonApi.getSelectLable("光遇", "热门物品", accountInfo.getAcclable());
            gameJson.put("gy_zhenxi",gy_zhenxi);
        }
        if (Objects.equals("永劫无间",accountInfo.getType())){
            gameJson.put("yjwj_sys_area",accountInfo.getRegional());
        }

        params.put("gameJson",gameJson);
    }


    private String getType (String type){


        if (Objects.equals("王者荣耀",type)){
            return "2";
        }

        if (Objects.equals("火影忍者",type)){
            return "12";
        }
        if (Objects.equals("和平精英",type)){
            return "26";
        }
        if (Objects.equals("原神",type)){
            return "28";
        }
        if (Objects.equals("穿越火线",type)){
            return "31";
        }
        if (Objects.equals("CF端游",type)){
            return "34";
        }
        if (Objects.equals("光遇",type)){
            return "37";
        }
        if (Objects.equals("永劫无间",type)){
            return "41";
        }
        if (Objects.equals("使命召唤",type)){
            return "46";
        }
        return null;
    }
}
